package cn.cmeizu.spring6.jdbc.tx.service.impl;

import cn.cmeizu.spring6.jdbc.tx.dao.BookDao;
import cn.cmeizu.spring6.jdbc.tx.dao.UserDao;
import cn.cmeizu.spring6.jdbc.tx.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author cmeizu
 * @date 2023/2/12 23:00
 */
@Service
public class BookServiceImpl implements BookService {
  @Autowired
  private BookDao bookDao;
  @Autowired
  private UserDao userDao;

  @Transactional(propagation = Propagation.REQUIRES_NEW)
  @Override
  public void buyBook(Integer bookId, Integer userId) throws InterruptedException {
    //TimeUnit.SECONDS.sleep(5);
    //根据图书id查询图书价格
    Long price = bookDao.getPriceByBookId(bookId);
    //更新图书表库存量 -1
    bookDao.updateStock(bookId);
    //更新用户表余额-图书价格
    userDao.updateBalance(userId, price);
  }
}
